package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.DayWork;
import cn.wolfcode.education.query.DayWorkQuery;
import cn.wolfcode.education.service.IDayWorkService;
import cn.wolfcode.education.util.DateUtil;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

@Controller
@RequestMapping("/daywork")
public class DayWorkController {
    @Autowired
    private IDayWorkService dayWorkService;

    @RequestMapping("")
    public String main(){
        return "daywork/list";
    }
    @RequestMapping("list")
    @ResponseBody
    public PageResult list(DayWorkQuery qo){
        if (qo.getWorkTime() == null){
            qo.setWorkTime(DateUtil.getBeginDate(new Date()));
        }
        System.out.println(qo.getWorkTime().toLocaleString());
        return dayWorkService.queryForList(qo);
    }










    //========================增删改操作================================
    /**
     * 管理员
     */
    @RequestMapping("/save")
    @ResponseBody
    public JSONResult save(DayWork dayWork){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.insert(dayWork);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg("程序异常");
        }
        return jsonResult;
    }
    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(DayWork dayWork){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.update(dayWork);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult save(Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.delete(id);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg("程序异常");
        }
        return jsonResult;
    }

    /**
     * 标记成功和失败
     */
    @RequestMapping("/success")
    @ResponseBody
    public JSONResult success(Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.success(id);
        }catch (RuntimeException e){
            String message = e.getMessage();
            jsonResult.setMsg(message);
        }
        return jsonResult;
    }
    @RequestMapping("/fail")
    @ResponseBody
    public JSONResult fail(Long id){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.fail(id);
        }catch (RuntimeException e){
            String message = e.getMessage();
            jsonResult.setMsg(message);
        }
        return jsonResult;
    }

    //=========================针对员工的操作================================
    @RequestMapping("/esave")
    @ResponseBody
    public JSONResult esave(DayWork dayWork){
        JSONResult jsonResult = new JSONResult();
        try {
            dayWorkService.esave(dayWork);
        }catch (RuntimeException e){
            e.printStackTrace();
            jsonResult.setMsg(e.getMessage());
        }
        return jsonResult;
    }
}
